﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSTaskDialog;
using System.Diagnostics;
using smx.Properties;
using System.Media;


namespace smx
{
    public partial class MasterForm : Form
    {

        //----------------------------------------------------------------------------------------------------------------------------
        //helper variables for sending to edit forms
        //----------------------------------------------------------------------------------------------------------------------------
        private string customerSelectedRow = "";
        private string supplierSelectedRow = "";
        private string componentSelectedRow = "";
        private string productSelectedRow = "";
        //----------------------------------------------------------------------------------------------------------------------------
        //Constructor
        //----------------------------------------------------------------------------------------------------------------------------
        public MasterForm()
        {
            InitializeComponent();
        }

    
        private void MasterForm_Load(object sender, EventArgs e)
        {
            if (Settings.Default.startupnoise == true)
            {
                SoundPlayer player = new SoundPlayer();
                player.Stream = Resources.magic_chime_alert_01;//start up sound in debug folder
                player.Play();
            }
        }

        //----------------------------------------------------------------------------------------------------------------------------
        #region navigation 
        //----------------------------------------------------------------------------------------------------------------------------

        //----------------------------------------------------------------------------------------------------------------------------
        //open customers panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void customerOpen()
        {
            customersPanel.Visible = true;
            suppliersPanel.Visible = false;
            componentsPanel.Visible = false;
            productsPanel.Visible = false;
            financePanel.Visible = false;
            homepanel.Visible = false;

            getCustomers();
        }
        
        //----------------------------------------------------------------------------------------------------------------------------
        //open the suppliers panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void openSuppliers()
        {
            componentsPanel.Visible = false;
            customersPanel.Visible = false;
            suppliersPanel.Visible = true;
            productsPanel.Visible = false;
            financePanel.Visible = false;
            homepanel.Visible = false;

            getSuppliers();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open the components panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void openComps()
        {
            componentsPanel.Visible = true;
            customersPanel.Visible = false;
            suppliersPanel.Visible = false;
            productsPanel.Visible = false;
            financePanel.Visible = false;
            homepanel.Visible = false;


            getComps();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open up the products panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void openProducts()
        {
            componentsPanel.Visible = false;
            customersPanel.Visible = false;
            suppliersPanel.Visible = false;                    
            productsPanel.Visible = true;
            financePanel.Visible = false;
            homepanel.Visible = false;

            getProducts();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open up the finance panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void openFinance()
        {
            componentsPanel.Visible = false;
            customersPanel.Visible = false;
            suppliersPanel.Visible = false;
            productsPanel.Visible = false;
            financePanel.Visible = true;
            homepanel.Visible = false;
            getFinances();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open up the home panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void openHome()
        {
            homepanel.Visible = true;
            componentsPanel.Visible = false;
            customersPanel.Visible = false;
            suppliersPanel.Visible = false;
            productsPanel.Visible = false;
            financePanel.Visible = false;
        }
        #endregion
        

        //----------------------------------------------------------------------------------------------------------------------------
        #region customers panel functions
        //----------------------------------------------------------------------------------------------------------------------------
        //----------------------------------------------------------------------------------------------------------------------------
        //get the customers grid filled
        //----------------------------------------------------------------------------------------------------------------------------
        public void getCustomers()
        {

            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            var q = from c in data.customers
                    select c;

            customersGrid.DataSource = q;
        }
        
        //----------------------------------------------------------------------------------------------------------------------------
        //opens up a new form with the relevant customer information
        //----------------------------------------------------------------------------------------------------------------------------

        private void customersGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            //get the current selected id value
            customerSelectedRow = customersGrid.CurrentRow.Cells[0].Value.ToString();

            //open up the edit box and pass the data to it
            customerEdit edit = new customerEdit(customerSelectedRow);
            edit.Show();
        }

        
        #endregion

        //----------------------------------------------------------------------------------------------------------------------------
        #region tool strip buttons
        //----------------------------------------------------------------------------------------------------------------------------
        private void homeToolBtn_Click(object sender, EventArgs e)
        {
            openHome();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //view customers 
        //----------------------------------------------------------------------------------------------------------------------------
        private void viewCustToolStrip_Click(object sender, EventArgs e)
        {
            customerOpen();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //add customers 
        //----------------------------------------------------------------------------------------------------------------------------
        private void custaddToolBtn_Click(object sender, EventArgs e)
        {
            customerEdit edit = new customerEdit();
            edit.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //delete a customer
        //----------------------------------------------------------------------------------------------------------------------------
        private void custdeleteToolBtn_Click(object sender, EventArgs e)
        {
            if (
           cTaskDialog.MessageBox(this,
               "Confirmation",
               "Are you sure you wish to delete?",
               "If you delete this record it will be permanently deleted. Are you sure you wish to delete this customer record?",
               eTaskDialogButtons.YesNo,
               eSysIcons.Question) == DialogResult.Yes)
            {
                try
                {

                    string s = customersGrid.CurrentRow.Cells[0].Value.ToString();
                    int id = Convert.ToInt32(s);
                    stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                    var q = (from c in data.customers
                             where c.id == id
                             select c).FirstOrDefault();

                    data.DeleteObject(q);
                    data.SaveChanges();
                    statusLbl.Text = "Record Deleted";
                    getCustomers();
                }
                catch (Exception err)
                {
                    cTaskDialog.MessageBox(this, "Error in Code",
                        "There was an error!",
                        "There was an error in attempting to delete the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                        err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                }
               
            }           

        }


        //----------------------------------------------------------------------------------------------------------------------------
        //view the suppliers
        //----------------------------------------------------------------------------------------------------------------------------
        private void viewSuppliersToolBtn_Click(object sender, EventArgs e)
        {
            openSuppliers();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //add a new supplier
        //----------------------------------------------------------------------------------------------------------------------------
        private void supplierAddToolBtn_Click(object sender, EventArgs e)
        {
            supplierEdit edit = new supplierEdit();
            edit.Show();
        }
        //----------------------------------------------------------------------------------------------------------------------------
        //delete the current supplier
        //----------------------------------------------------------------------------------------------------------------------------
        private void supplierDeleteToolBtn_Click(object sender, EventArgs e)
        {

            if (
           cTaskDialog.MessageBox(this,
               "Confirmation",
               "Are you sure you wish to delete?",
               "If you delete this record it will be permanently deleted. Are you sure you wish to delete this customer record?",
               eTaskDialogButtons.YesNo,
               eSysIcons.Question) == DialogResult.Yes)
            {
                try
                {
                    string delete = suppliersGrid.CurrentRow.Cells[0].Value.ToString();
                    int id = Convert.ToInt32(delete);
                    stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                    var del = (from s in data.suppliers
                               where s.id == id
                               select s).FirstOrDefault();

                    data.DeleteObject(del);
                    data.SaveChanges();
                    getSuppliers();
                    statusLbl.Text = "Deleted Record";

                }
                catch (Exception err)
                {
                    cTaskDialog.MessageBox(this, "Error in Code",
                           "There was an error!",
                           "There was an error in attempting to Delete the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                           err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                }
            }
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //view the components
        //----------------------------------------------------------------------------------------------------------------------------
        private void viewCompsToolStripBtn_Click(object sender, EventArgs e)
        {
            openComps();
        }
        //----------------------------------------------------------------------------------------------------------------------------
        //open the add component dialog
        //----------------------------------------------------------------------------------------------------------------------------
        private void compAddToolBtn_Click(object sender, EventArgs e)
        {
            compEdit c = new compEdit();
            c.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //delete the current component record
        //----------------------------------------------------------------------------------------------------------------------------
        private void compDeleteTooBtn_Click(object sender, EventArgs e)
        {
            deleteComp();
        }
        
        //----------------------------------------------------------------------------------------------------------------------------
        //get the products grid as focused
        //----------------------------------------------------------------------------------------------------------------------------
        private void viewProductsToolBtn_Click(object sender, EventArgs e)
        {
            openProducts();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //add a product, open up the dialog
        //----------------------------------------------------------------------------------------------------------------------------
        private void addprodsToolBtn_Click(object sender, EventArgs e)
        {
            productEdit edit = new productEdit();
            edit.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //delete the currently selected product
        //----------------------------------------------------------------------------------------------------------------------------
        private void deleteprodToolBtn_Click(object sender, EventArgs e)
        {
            deleteProduct();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open the component assign dialog
        //----------------------------------------------------------------------------------------------------------------------------
        private void componentsAssignToolStripMenuItem_Click(object sender, EventArgs e)
        {
            compAssignForm cas = new compAssignForm();
            cas.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open up the finance grid
        //----------------------------------------------------------------------------------------------------------------------------
        private void viewFinanceToolBtn_Click(object sender, EventArgs e)
        {
            openFinance();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open the transactions dialog
        //----------------------------------------------------------------------------------------------------------------------------
        private void addATransactionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            transactions t = new transactions();
            t.Show();
        }

        private void changeSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangeSettings c = new ChangeSettings();
            c.Show();
        }

        #endregion


        //----------------------------------------------------------------------------------------------------------------------------
        #region supplier panel functions
        //----------------------------------------------------------------------------------------------------------------------------

        //----------------------------------------------------------------------------------------------------------------------------
        //load the suppliers records into the grid
        //----------------------------------------------------------------------------------------------------------------------------
        public void getSuppliers()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            var q = from s in data.suppliers
                    select s;

            suppliersGrid.DataSource = q;
        }
        //----------------------------------------------------------------------------------------------------------------------------
        //edits the details on the grid that the user double clicks on
        //----------------------------------------------------------------------------------------------------------------------------
        private void suppliersGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            supplierSelectedRow = suppliersGrid.CurrentRow.Cells[0].Value.ToString();

            supplierEdit edit = new supplierEdit(supplierSelectedRow);
            edit.Show();
        }


        #endregion

        //----------------------------------------------------------------------------------------------------------------------------
        #region main menu functions
        //----------------------------------------------------------------------------------------------------------------------------
        //----------------------------------------------------------------------------------------------------------------------------
        //open the add new customer grid
        //----------------------------------------------------------------------------------------------------------------------------
        private void customerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            customerEdit edit = new customerEdit();
            edit.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //view the customers
        //----------------------------------------------------------------------------------------------------------------------------
        private void customersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            customerOpen();
        }
        
        //----------------------------------------------------------------------------------------------------------------------------
        //get the suppliers panel open
        //----------------------------------------------------------------------------------------------------------------------------
        private void suppliersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openSuppliers();
        }


        //----------------------------------------------------------------------------------------------------------------------------
        //add a new supplier
        //----------------------------------------------------------------------------------------------------------------------------
        private void supplierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            supplierEdit edit = new supplierEdit();
            edit.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //show the components in the grid
        //----------------------------------------------------------------------------------------------------------------------------
        private void componentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openComps();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //open the add form for components
        //----------------------------------------------------------------------------------------------------------------------------
        private void componentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            compEdit c = new compEdit();
            c.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //gets information about the software displayed in one of hedley's task dialogs
        //----------------------------------------------------------------------------------------------------------------------------
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cTaskDialog.MessageBox(this, "Casserly Programming Information",
                            "Stock Manager Express",
                            "This software was created by Casserly Programming for free distribution. It is to help with stock levels of smaller companies. For support either go to www.casserlyprogramming.com/forum or contact casserly programming",
                             "Version: 1.0.0; Language: c#; Database:Sqlite; environment: .net3.5 Winforms","About", "", eTaskDialogButtons.OK, eSysIcons.Information, eSysIcons.Information);
           
        }


        //----------------------------------------------------------------------------------------------------------------------------
        //opens up the browser on the forum page
        //----------------------------------------------------------------------------------------------------------------------------
        private void forumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://www.casserlyprogramming.com/forum", null);
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //opens up teamviewer after warning the user about the support process if team viewer is not on the system it will download it
        //----------------------------------------------------------------------------------------------------------------------------
        private void remoteSupportToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                cTaskDialog.MessageBox(this,
            "Remote Support",
            "Remote Support Via Team Viewer",
            "Please use team viewer so that we can help you with remote support, team viewer will start now, please give one of our support team the id and password so that they can help you.",
            eTaskDialogButtons.OK,
            eSysIcons.Information);
                Process.Start(@"C:\Program Files\TeamViewer\Version6\TeamViewer.exe");

            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this,
         "Remote Support",
         "Remote Support Via Team Viewer",
         "We have noticed that Team Viewer is not on your system, please press run, or save and then run on the following download, then install team viewer to get remote support.",
         eTaskDialogButtons.OK,
         eSysIcons.Warning);
                Process.Start("http://www.teamviewer.com/download/TeamViewer_Setup.exe");
            }
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //view the products panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void productsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openProducts();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //add a new product 
        //----------------------------------------------------------------------------------------------------------------------------
        private void productToolStripMenuItem_Click(object sender, EventArgs e)
        {
            productEdit edit = new productEdit();
            edit.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //opens the components assignment dialog
        //----------------------------------------------------------------------------------------------------------------------------
        private void componentsassignMenuItem_Click(object sender, EventArgs e)
        {
            compAssignForm form = new compAssignForm();
            form.Show(); 
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //opens the transactions menu
        //----------------------------------------------------------------------------------------------------------------------------
        private void transToolMenuBtn_Click(object sender, EventArgs e)
        {
            transactions t = new transactions();
            t.Show();
        }

        private void financeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFinance();
        }


        //----------------------------------------------------------------------------------------------------------------------------
        //opens the home panel
        //----------------------------------------------------------------------------------------------------------------------------
        private void homeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openHome();
        }
 
        private void homeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            openHome();
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        #endregion


        //----------------------------------------------------------------------------------------------------------------------------
        #region component panel functions
        //----------------------------------------------------------------------------------------------------------------------------

        //----------------------------------------------------------------------------------------------------------------------------
        //get the components into the grid
        //----------------------------------------------------------------------------------------------------------------------------

        public void getComps()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            var q = from c in data.compSuppGets
                    select new
                    {
                        c.id,
                        c.name,
                        c.notes,
                        c.qtyinstock,
                        c.lowlevel,
                        price = c.value,
                        supplierFirstName = c.firstname,
                        supplierLastName = c.lastname
                    };

            compsGrid.DataSource = q;
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //get the relevant record in the edit form
        //----------------------------------------------------------------------------------------------------------------------------
        private void compsGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            componentSelectedRow = compsGrid.CurrentRow.Cells[0].Value.ToString();

            compEdit edit = new compEdit(componentSelectedRow);
            edit.Show();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //delete the record, TODO make such fucntions for customes and suppliers
        //----------------------------------------------------------------------------------------------------------------------------
        private void deleteComp()
        {
            if (
        cTaskDialog.MessageBox(this,
            "Confirmation",
            "Are you sure you wish to delete?",
            "If you delete this record it will be permanently deleted. Are you sure you wish to delete this customer record?",
            eTaskDialogButtons.YesNo,
            eSysIcons.Question) == DialogResult.Yes)
            {
                try
                {
                    string delete = compsGrid.CurrentRow.Cells[0].Value.ToString();
                    long id = Convert.ToInt64(delete);
                    stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                    var del = (from s in data.components
                               where s.id == id
                               select s).FirstOrDefault();

                    data.DeleteObject(del);
                    data.SaveChanges();
                    getComps();
                    statusLbl.Text = "Deleted Record";

                }
                catch (Exception err)
                {
                    cTaskDialog.MessageBox(this, "Error in Code",
                           "There was an error!",
                           "There was an error in attempting to Delete the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                           err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                }
            }
        }

        #endregion


        //----------------------------------------------------------------------------------------------------------------------------
        #region product panel functions
        //----------------------------------------------------------------------------------------------------------------------------

        //----------------------------------------------------------------------------------------------------------------------------
        //filling the products grid
        //----------------------------------------------------------------------------------------------------------------------------
        public void getProducts()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            var q = from p in data.products
                    select p;

            productsGrid.DataSource = q;
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //get the selected product and send it to the edit form
        //----------------------------------------------------------------------------------------------------------------------------
        private void productsGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
               if (e.RowIndex == -1)
                return;

            productSelectedRow = productsGrid.CurrentRow.Cells[0].Value.ToString();

            productEdit edit = new productEdit(productSelectedRow);
            edit.Show();
        }


        //----------------------------------------------------------------------------------------------------------------------------
        //the function for deleting products
        //----------------------------------------------------------------------------------------------------------------------------
        private void deleteProduct()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            long prodid = Convert.ToInt64(productsGrid.CurrentRow.Cells[0].Value);

            var q = (from p in data.products
                     where p.id == prodid
                     select p).FirstOrDefault();

            data.DeleteObject(q);
            data.SaveChanges();
            getProducts();
        }
        #endregion


        //----------------------------------------------------------------------------------------------------------------------------
        #region finance panel functions
        //----------------------------------------------------------------------------------------------------------------------------
        private void getFinances()
        {
            financeLbl.Text = "£" + Settings.Default.finance.ToString();

            stockmanagerexpressEntities data = new stockmanagerexpressEntities();
            var q = from f in data.viewtransactions
                    select f;

            financeGrid.DataSource = q;

        }

 
        private void financeGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            if (e.ColumnIndex == financeGrid.Columns["id"].Index)
            {
                long rowid = (long)e.Value;

                if (rowid > 100000)
                {
                    financeGrid.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Green;
                }
                else
                {
                    financeGrid.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
                }
            }
        }


        #endregion

        //----------------------------------------------------------------------------------------------------------------------------
        #region homepanel functions
        //----------------------------------------------------------------------------------------------------------------------------
       
        
        
        private void financeBtn_Click(object sender, EventArgs e)
        {
            openFinance();
        }

        private void customerBtn_Click(object sender, EventArgs e)
        {
            customerOpen();
        }

        private void supplierBtn_Click(object sender, EventArgs e)
        {
            openSuppliers();
        }

        private void componentBtn_Click(object sender, EventArgs e)
        {
            openComps();
        }

        private void prodsBtn_Click(object sender, EventArgs e)
        {
            openProducts();
        }

      
       
        private void helpBtn_Click(object sender, EventArgs e)
        {
            Process.Start(@"http://www.casserlyprogramming.com/forum");
        }

        
        
        #endregion



     
  













    }

}
